package 链表;

import java.util.Stack;

/**
 * 题目描述：
 *
 * 输入一个链表的头结点，从尾到头反过来打印出每个结点的值。节点如下定义：
 */
public class 从头到尾打印链表 {
    public static void main(String[] args) {
        Node node = createNodeList();
        Stack<Integer> stack = new Stack<>();
        while (node != null){
            stack.add(node.getValue());
            node = node.tail;
        }

        while (!stack.empty()){
            System.out.println(stack.pop());
        }
    }

    public static  Node createNodeList(){
        Node head = new Node();
        Node prev = head;
        head.setValue(1);
        for (int i = 2; i < 10; i++) {
            Node node = new Node();
            node.setValue(i);
            node.setPrevN(prev);
            node.getPrevN().setTail(node);
            prev = node;
        }
        return head;
    }
    public static class Node {
        int value;
        private Node prevN;
        private Node tail;

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }

        public Node getPrevN() {
            return prevN;
        }

        public void setPrevN(Node prevN) {
            this.prevN = prevN;
        }

        public Node getTail() {
            return tail;
        }

        public void setTail(Node tail) {
            this.tail = tail;
        }
    }
}
